<?php
/**
 * Created by PhpStorm.
 * User: G
 * Date: 2021/3/23
 * Time: 下午2:12
 */

namespace app\api\controller;

use app\admin\model\profit\Agentapply;
use app\common\controller\Api;
use think\Exception;

class Agent extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 代理商申请
     * @author G
     * @date 2021-03-23 下午2:19
     */
    public function apply()
    {
        try {
            $p_id = $this->request->post('p_id', 0);
            $p_name = $this->request->post('p_name', '');
            $c_id = $this->request->post('c_id', 0);
            $c_name = $this->request->post('c_name', '');
            $cc_id = $this->request->post('cc_id', 0);
            $cc_name = $this->request->post('cc_name', '');
            $p_mobile = $this->request->post('p_mobile', '');

            $city = $p_name . '/' . $c_name . '/' . $cc_name;

            $name = $this->request->post('name', '');
            $mobile = $this->request->post('mobile', '');

            $shop_id = $this->request->post('shop_id', 0);
            $remarks = $this->request->post('remarks', '');

            if (!$name || !$mobile) {
                throw new Exception('参数缺失');
            }

            // 代理类型:3=区代,4=市代
            $type = $this->request->post('type');

            // 验证用户身份
//            if ($this->auth->getUser()->group_id != \app\common\model\User::GROUP_NORMAL) {
//                throw new Exception('身份信息异常');
//            }

            // 邀请人手机号
            if ($p_mobile) {
                $p_user = \app\admin\model\User::where(['mobile' => $p_mobile])->find();
                if (!$p_user || $p_user['group_id'] < 2) {
                    throw new Exception('无邀请人信息');
                }

                // 验证用户身份
                if ($this->auth->getUser()->mobile == $p_mobile) {
                    throw new Exception('邀请人不能是本人手机号');
                }
            }

            // 查询该省市区是否存在代理
            $agent_info = \app\admin\model\User::where([
                'province_name' => $p_name,
                'city_name' => $c_name,
                'county_name' => $cc_name,
                'group_id' => $type
            ])->find();
            if ($agent_info) {
                throw new Exception('代理已存在');
            }

            $insert_res = Agentapply::insert([
                'uid' => $this->auth->getUser()->id,
                'p_uid' => isset($p_user) ? $p_user['id'] : 0,
                'name' => $name ? $name : '',
                'mobile' => $mobile ? $mobile : '',
                'p_mobile' => $p_mobile ? $p_mobile : '',
                'city' => $city ? $city : '',
                'type' => $type,
                'status' => 0,
                'p_id' => $p_id ? $p_id : 0,
                'p_name' => $p_name ? $p_name : '',
                'c_id' => $c_id ? $c_id : 0,
                'c_name' => $c_name ? $c_name : '',
                'cc_id' => $cc_id ? $cc_id : 0,
                'cc_name' => $cc_name ? $cc_name : '',
                'shop_id' => $shop_id,
                'remarks' => $remarks,
                'createtime' => time(),
                'updatetime' => time(),
            ]);

            if(!$insert_res) {
                throw new Exception('申请失败');
            }
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('申请成功', []);

    }
}